package com.jy.adscm.operation.cg.controller;


import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.*;
import com.jy.adscm.operation.cg.service.CgBomSupplierService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * bom与供应商 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-07-07
 */
@RestController
@RequestMapping("adscm/operation/cg/cgBomSupplierSupplier")
public class CgBomSupplierController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    CgBomSupplierService cgBomSupplierService;

    /**
     * 增加bom信息
     * @param cgBomSupplier
     * @param request
     * @return
     */
    @AdscmLogger("增加bom与供应商关系信息")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody CgBomSupplier cgBomSupplier, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        cgBomSupplier.setCreator(token.getUser().getId().toString());
        cgBomSupplier.setCreateTime(new Date());
        boolean insert = cgBomSupplierService.insert(cgBomSupplier);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }

    /**
     * 修改bom信息
     * @param cgBomSupplier
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @AdscmLogger("修改bom与供应商关系信息")
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody CgBomSupplier cgBomSupplier, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        cgBomSupplier.setCreator(token.getUser().getId().toString());
        cgBomSupplier.setCreateTime(new Date());
        boolean result = cgBomSupplierService.updateById(cgBomSupplier);
        return  new SimpleCoreResponse("200","修改成功",result);
    }


    /**
     * 根据图纸id查询bom信息
     * @param bid
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectByBid/{bid}")
    public SimpleCoreResponse selectByBid(@PathVariable Integer bid , HttpServletRequest request) throws AdscmSystemException {
        List<CgBomSupplier> cgBomSuppliers = cgBomSupplierService.selectByBid(bid);
        return  new SimpleCoreResponse("200","查询成功",cgBomSuppliers);
    }
    /**
     * 根据图纸id查询bom信息
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectDrawBoms")
    public SimpleCoreResponse selectDrawBoms( HttpServletRequest request) throws AdscmSystemException {
        List<JyDrawingType> jyDrawingTypes = cgBomSupplierService.selectDrawBoms();
        return  new SimpleCoreResponse("200","查询成功",jyDrawingTypes);
    }
    /**
     * 根据图纸id查询bom信息
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectBoms")
    public SimpleCoreResponse selectBoms(@RequestBody QueryCgBom queryCgBom, HttpServletRequest request) throws AdscmSystemException {
        BomList bomList= cgBomSupplierService.selectBom(queryCgBom);
        return  new SimpleCoreResponse("200","查询成功",bomList);
    }
    @RequestMapping("selectBom")
    public SimpleCoreResponse selectBom(@RequestBody QueryCgBom queryCgBom, HttpServletRequest request) throws AdscmSystemException {
        BomList bomList= cgBomSupplierService.querybom(queryCgBom);
        return  new SimpleCoreResponse("200","查询成功",bomList);
    }
    @RequestMapping("queryBom")
    public SimpleCoreResponse queryBom(@RequestBody QueryCgBom queryCgBom, HttpServletRequest request) throws AdscmSystemException {
        BomList bomList= cgBomSupplierService.queryboms(queryCgBom);
        return  new SimpleCoreResponse("200","查询成功",bomList);
    }




    @RequestMapping("selectSupplierbyId")
    public SimpleCoreResponse selectSupplierbyId(@RequestBody QueryCgBom queryCgBom, HttpServletRequest request) throws AdscmSystemException {
       List<CgSupplier> cgSuppliers=cgBomSupplierService.selectSupplierbyId(queryCgBom);
        return  new SimpleCoreResponse("200","查询成功",cgSuppliers);
    }
    @RequestMapping("updateBom")
    public SimpleCoreResponse updateBom(@RequestBody CgBom cgBom, HttpServletRequest request) throws AdscmSystemException {
       cgBomSupplierService.updateBom(cgBom);
        return  new SimpleCoreResponse("200","查询成功",true);
    }
    @RequestMapping("test")
    public SimpleCoreResponse test( HttpServletRequest request) throws AdscmSystemException {
       cgBomSupplierService.test();
        return  new SimpleCoreResponse("200","查询成功",true);
    }






}

